package com.chandra.pos.web.payment.action;

import com.chandra.pos.EntityNotFoundException;
import com.chandra.pos.dao.entity.CustomerEntity;
import com.chandra.pos.dao.entity.PaymentEntity;
import com.chandra.pos.dao.entity.PaymentTypeEntity;
import com.chandra.pos.security.UserDetailsImpl;
import com.chandra.pos.service.PaymentManager;
import com.chandra.pos.service.UserManagement;
import com.chandra.pos.web.CRUDAction;
import com.opensymphony.xwork2.ActionSupport;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * UserEntity: root
 * Date: 6/4/12
 * Time: 11:26 PM
 * To change this template use File | Settings | File emplates.
 */
public class PaymentAction extends ActionSupport implements CRUDAction {


    private UserManagement userManagementImpl;


    public UserManagement getUserManagementImpl() {
        return userManagementImpl;
    }

    public void setUserManagementImpl(UserManagement userManagementImpl) {
        this.userManagementImpl = userManagementImpl;
    }

    private List<PaymentEntity> paymentEntities;


    private PaymentEntity paymentEntity;

    @Autowired
    private PaymentManager paymentManager;

    private long customerId;

    private BigDecimal paymentAmount;

    private long paymentType;

    private String comment;

    private String paymentDate;

    private long orderId;

    private long id;

    @Override
    public String doNew() {

        return INPUT;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String doDelete() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String doList() {
        DateTime date = new DateTime().toDateMidnight().toDateTime();
        Date daTE = new Date();

        DateTime tomorrow = date.plusDays(1);


        this.paymentEntities = paymentManager.getPaymentsByDate(date.getMillis(), tomorrow.getMillis());

        return SUCCESS;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String doUpdate() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String doEdit() {
        this.paymentEntity = paymentManager.getPayment(id);

        return INPUT;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String doSave() {
        CustomerEntity customer = null;
        try {
            customer = userManagementImpl.getCustomer(customerId);
        } catch (EntityNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        PaymentEntity paymentEntity = new PaymentEntity();
        paymentEntity.setCustomer(customer);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        long uid = 0;
        if (principal instanceof UserDetailsImpl) {
            paymentEntity.setEmployee(((UserDetailsImpl) principal).getUser());

        }
        paymentEntity.setAmount(paymentAmount);
        paymentEntity.setNote(comment);
        paymentEntity.setPaymentDate(new Date().getTime());
        paymentEntity.setType(new PaymentTypeEntity(paymentType));
        paymentEntity=paymentManager.savePayment(paymentEntity);
        return SUCCESS;  //To change body of implemented methods use File | Settings | File Templates.
    }


    public String doReceipt() {
        this.paymentEntity = paymentManager.getPayment(id);

        return SUCCESS;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<PaymentEntity> getPaymentEntities() {
        return paymentEntities;
    }

    public void setPaymentEntities(List<PaymentEntity> paymentEntities) {
        this.paymentEntities = paymentEntities;
    }

    public PaymentEntity getPaymentEntity() {
        return paymentEntity;
    }

    public void setPaymentEntity(PaymentEntity paymentEntity) {
        this.paymentEntity = paymentEntity;
    }


    public PaymentManager getPaymentManager() {
        return paymentManager;
    }

    public void setPaymentManager(PaymentManager paymentManager) {
        this.paymentManager = paymentManager;
    }

    public long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }

    public BigDecimal getPaymentAmount() {
        return paymentAmount;
    }

    public void setPaymentAmount(BigDecimal paymentAmount) {
        this.paymentAmount = paymentAmount;
    }

    public long getPaymentType() {
        return paymentType;
    }

    public void setPaymentType(long paymentType) {
        this.paymentType = paymentType;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getPaymentDate() {
        return paymentDate;
    }

    public void setPaymentDate(String paymentDate) {
        this.paymentDate = paymentDate;
    }

    public long getOrderId() {
        return orderId;
    }

    public void setOrderId(long orderId) {
        this.orderId = orderId;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}
